package fristphase.submarinegame08complete;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;
    public static final int RUNNING = 0;
    public static final int PAUSE = 1;
    public static final int GAME_OVER = 2;
    private int state = RUNNING;

    //战舰
    private BattleShip ship = new BattleShip();

    //潜艇
    private SeaObject[] submarines = {};

    //水雷
    private Mine[] mines = {};

    //炸弹
    private Bomb[] bombs = {};

    //随机生成潜艇
    private int observeSubmarineCount = 0;
    private int mineSubmarineCount = 0;
    private int torpedoSubmarineCount = 0;
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type= rand.nextInt(20);
        //生成侦察潜艇的概率
        if(type<10){
            observeSubmarineCount++;
            return new ObserveSubmarine(observeSubmarineCount);
        }else if(type<15){//生成水雷潜艇的概率
            mineSubmarineCount++;
            return new MineSubmarine(mineSubmarineCount);
        }else{//生成鱼雷潜艇的概率
            torpedoSubmarineCount++;
            return new TorpedoSubmarine(torpedoSubmarineCount);
        }
    };


    //潜艇入场
    private int subEnterIndex = 0;
    public void submarineEnterAction(){
        subEnterIndex++;
        if(subEnterIndex%40 == 0){//每400ms生成一个潜艇
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }

    //水雷入场
    private int mineEnterIndex = 0;
    public void mineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100 == 0){
            for (int i = 0; i < submarines.length; i++) {
                if(submarines[i] instanceof MineSubmarine){
                    MineSubmarine ms = (MineSubmarine) submarines[i];
                    Mine obj = ms.shootMine();//获得水雷对象
                    mines = Arrays.copyOf(mines,mines.length+1);
                    mines[mines.length-1] = obj;
                }
            }
        }
    }


    //海洋对象移动
    public void moveAction(){
        for (int i = 0; i < submarines.length; i++) {
            submarines[i].move();
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].move();
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].move();
        }
    }

    //删除越界对象
    public void outOfBoundsAction(){
        for (int i = 0; i < submarines.length; i++) {
            if(submarines[i].isOutOfBounds() || submarines[i].isDead()){
                submarines[i] = submarines[submarines.length-1];
                submarines = Arrays.copyOf(submarines,submarines.length-1);
            }
        }
        for (int i = 0; i < mines.length; i++) {
            if(mines[i].isOutOfBounds() || mines[i].isDead()){
                mines[i] = mines[mines.length-1];
                mines = Arrays.copyOf(mines, mines.length-1);
            }
        }
        for (int i = 0; i < bombs.length; i++) {
            if(bombs[i].isOutOfBounds() ||bombs[i].isDead()){
                bombs[i] = bombs[bombs.length-1];
                bombs = Arrays.copyOf(bombs,bombs.length-1);
            }
        }
    }

    //玩家得分
    private int score = 0;
    //深水炸弹与潜艇的碰撞
    private void  bombBangAction(){
        for (int i = 0; i < bombs.length; i++) {
            Bomb b = bombs[i];
            for (int j = 0; j < submarines.length; j++) {
                SeaObject s = submarines[j];
                if(b.isLive() && s.isLive() && s.isHit(b)){
                    b.goDead();
                    s.goDead();
                    if(s instanceof EnemyScore){
                        EnemyScore es = (EnemyScore) s;
                        score += es.getScore();
                    }
                    if(s instanceof EnemyLife){
                        EnemyLife el = (EnemyLife) s;
                        int life = el.getLife();
                        ship.addLife(life);
                    }
                }
            }
        }
    }

    //水雷与战舰相撞
    private void mineBangAction(){
        for (int i = 0; i < mines.length; i++) {
            Mine m = mines[i];
            if(ship.isHit(m)){
                mines[i].goDead();
                ship.subtractLife();
            }
        }
    }

    //游戏结束,通过检测战舰生命值
    public void gameOver(){
        if(ship.getLife() <= 0){
            this.state = GAME_OVER;
        }
    }

    //启动程序运行
    public void action(){
        KeyAdapter k = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_P){
                    if(state == PAUSE){
                        state = RUNNING;
                    }else if(state == RUNNING){
                        state = PAUSE;
                    }
                }
                if(state == RUNNING){//判断状态
                    if(e.getKeyCode() == KeyEvent.VK_SPACE){
                        Bomb obj = ship.shootBomb();//深水炸弹入场
                        bombs = Arrays.copyOf(bombs,bombs.length+1);
                        bombs[bombs.length-1] = obj;
                    }
                    if(e.getKeyCode() == KeyEvent.VK_LEFT){
                        ship.moveLeft();
                    }
                    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                        ship.moveRight();
                    }
                }
            }
        };
        this.addKeyListener(k);

        if (state == RUNNING){//判断状态
            Timer timer = new Timer();
            int myInterval = 10;
            timer.schedule(new TimerTask(){
                public void run(){
                    if(state == RUNNING){
                        submarineEnterAction();//潜艇入场
                        mineEnterAction();//水雷入场
                        moveAction();//海洋对象移动
                        outOfBoundsAction();//删除越界对象
                        bombBangAction();//炸弹碰撞消失功能加入
                        mineBangAction();//水雷碰撞减生命功能加入
                        gameOver();//检测游戏是否结束
                        repaint();
                    }

                }
            },myInterval,myInterval);
        }

    }



    public void paint(Graphics g){
        Images.sea.paintIcon(null,g,0,0);//画海洋图
        ship.paintImage(g);
        for (int i = 0; i < submarines.length; i++) {
            submarines[i].paintImage(g);//画潜艇
            submarines[i].position();//输出潜艇的坐标
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].paintImage(g);//画水雷
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].paintImage(g);//画炸弹
        }
        g.drawString("SCORE："+score,200,50);
        g.drawString("LIFE："+ ship.getLife(),400,50);

        if(state == GAME_OVER){
            Images.gameOver.paintIcon(null,g,0,0);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        //设置焦点
        world.setFocusable(true);
        frame.add(world);
        //设置同步开关,EXIT_ON_CLOSE是结束窗口活动
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //HEIGHT = 479 ,WIDTH = 641
        frame.setSize(WIDTH+6,HEIGHT+29);
        //在屏幕中间生成窗口
        frame.setLocationRelativeTo(null);
        //设置窗口是否可以调整大小
        frame.setResizable(true);
        //设置窗体可见
        frame.setVisible(true);
        world.action();
    }
}
